package activitishizhan.userAndGroup;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.junit.Test;

import java.util.List;

public class IdentifyServiceTest {

    /**
     * 获取流程引擎
     */
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    /**
     * 创建用户和组以及关联关系
     */
    @Test
    public void testUserAndGroupMemership(){
        //创建组
        IdentityService identityService = processEngine.getIdentityService();
        Group deptLeader = identityService.newGroup("deptLeader");
        deptLeader.setName("部门领导");
        deptLeader.setType("assignment");
        identityService.saveGroup(deptLeader);
        //创建用户
        User user = identityService.newUser("张三");
        user.setFirstName("张");
        user.setLastName("三");
        user.setEmail("1756380197@qq.com");
        identityService.saveUser(user);
        //把用户添加到组
        identityService.createMembership("张三","deptLeader");
        //查询deptLeader组的用户
        List<User> leaderList =  identityService.createUserQuery().memberOfGroup("deptLeader")
                .list();
        if(null != leaderList && leaderList.size() > 0){
            for(User temp : leaderList){
                System.out.println(temp.getId());
            }
        }
        //查询user张三所属的组
        Group group = identityService.createGroupQuery().groupMember("张三").singleResult();
        System.out.println(group.getId());
    }

}
